<?php

namespace app\api\model;

use app\common\library\Distance;
use think\Model;


class Store extends BaseModel
{
    // 表名
    protected $name = 'hk_store';
    protected $hidden = ['status', 'createtime'];
    protected $resultSetType = 'collection';
    protected static $curLongitude = 0;
    protected static $curLatitude = 0;

    protected function base($query)
    {
//        $mpId = get_api_applet_id();
        $globalKey = [
//            'mp_id'=>$mpId,
            'status'=>'normal',
        ];
        $query->where($globalKey);
    }

    // 追加属性
//    protected $append = [
//        'distance'
//    ];

	//关联店铺分类id
	public function catgory()
	{
		return $this->belongsTo('StoreCat','cat_id','id');
	}

	//店铺关联卡券
	public function card()
	{
		return $this->hasMany('WechatCard','store_id','id')->field(['id','logo_url','title','reduce_cost','sub_title','quantity','begin_timestamp','end_timestamp','store_id'])
            ->where('card_id','<>',0)
            ->where('quantity','<>','0')
            ->where('status','neq','CARD_STATUS_DELETE')
            ->where('end_timestamp','>',time());
	}

    public static function getList($map = [], $longitude = 0, $latitude = 0,$page = 1, $row = 6)
    {
        $longitude != 0 && self::$curLongitude = $longitude;
        $longitude != 0 && self::$curLatitude = $latitude;
        $data = self::where($map)
            ->page($page,$row)
            ->select();
        if(!empty($data)){
            $res['data'] = $data->toArray();
            $res['slides'] = [];
            $sImagesArr = array_column($res['data'],'s_images');
            foreach ($sImagesArr as $item) {
                array_push($res['slides'],$item[array_rand($item)]);
            }
        }else{
            $res = [];
        }
        return $res;
    }

    public static function getById($id)
    {
        $lng = input('lng',0);
        $lat = input('lat',0);
        $field = ['id','s_name','s_image','s_score','s_address','service_phone','pos_longitude','pos_latitude','detail','s_images','cat_id'];
        $field[] = 'get_distance('.$lng.',"'.$lat.'",pos_longitude,pos_latitude) as distance';
        $data = self::with('card')
		    ->where('id', $id)
			->field($field)
			->find();
        $res['data'] = empty($data)? [] :$data->toArray();
//        foreach($res['data'] as $key=>&$val){
//            $val['s_images'] = explode(',',$val['s_images']);
//        }
        $cat = StoreCat::where(['cat_name' => '推荐'])->find();
        $res['data']['advert'] = $cat['id'];
        //设置s_image图片
        $res['data']['s_image'] = $res['data']['s_images'][0];
        //删掉介绍图第一张
        array_shift($res['data']['s_images']);
        return $res;
    }

    public function getSImagesAttr($value)
    {
        return explode(',', $value);
    }

//    //获取经纬度距离
//    public function getDistanceAttr($value,$data)
//    {
//        if(self::$curLongitude!=0&&self::$curLatitude!=0){
//            $distance = Distance::get(
//                self::$curLongitude,self::$curLatitude,
//                $data['pos_longitude'],$data['pos_latitude'],2);
//        }else{
//            $distance = '--';
//        }
//        return $distance . 'km';
//    }

	//获取距离用户最近的附近门店
	public static function nearbyList($lng,$lat)
	{
		$distance = 'get_distance('.$lng.',"'.$lat.'",pos_longitude,pos_latitude) as distance';
		$arr = ['id','s_name','s_address',$distance];
        $res['data'] = self::field($arr)
			->order(['distance'=>'asc'])
			->limit(10)
			->select();
		return $res;
	}
}
